/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import org.aergia.vinny.database.pojo.Volunteer;
import org.aergia.vinny.database.pojo.VolunteerGroup;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.util.DialogUtil;
import org.aergia.vinny.gui.util.FontUtil;
import org.aergia.vinny.gui.util.LabelPanel;
import org.aergia.vinny.util.DateUtil;
import org.freixas.jcalendar.JCalendarCombo;

/**
 * @author Fred
 */
public class ServiceReportDialog extends JDialog implements ActionListener {

	public enum ReportGroup {
		All, Group, Individual
	}

	class LocalFileFilter extends FileFilter {

		@Override
		public boolean accept(final File f) {
			if (f.isDirectory()) {
				return true;
			}
			if (f.getName().endsWith(".pdf")) {
				return f.canWrite();
			}
			return false;
		}

		@Override
		public String getDescription() {
			return "PDF file";
		}
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final VinnyController controller;
	private JCalendarCombo endDate;
	private String fileName;
	private JComboBox reportGroup;
	private JComboBox reportSelection;
	private JButton saveButton;
	private JCalendarCombo startDate;
	private JCheckBox summaryReport;

	public ServiceReportDialog(final VinnyController controller) {
		this.controller = controller;
		this.setModalityType(ModalityType.APPLICATION_MODAL);
		this.initializeComponents();
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		if (e.getSource() == this.reportGroup) {
			this.reportSelection.removeAllItems();
			final ReportGroup selectedItem = (ReportGroup) this.reportGroup.getSelectedItem();
			if (selectedItem == ReportGroup.All) {
				this.reportSelection.setEnabled(false);
			} else if (selectedItem == ReportGroup.Group) {
				this.reportSelection.setEnabled(true);
				final Collection<? extends VolunteerGroup> groups = this.controller.getGroups();
				for (final VolunteerGroup group : groups) {
					this.reportSelection.addItem(group.getName());
				}
			} else {
				this.reportSelection.setEnabled(true);
				final Collection<? extends Volunteer> volunteers = this.controller.getVolunteers();
				for (final Volunteer volunteer : volunteers) {
					this.reportSelection.addItem(volunteer);
				}
			}
			this.reportSelection.invalidate();
		} else if (e.getSource() == this.saveButton) {
			if (!this.verify()) {
				DialogUtil.alert(this, "Invalid start/end date", "Invalid date");
			} else {
				final JFileChooser chooser = new JFileChooser();
				chooser.setFileFilter(new LocalFileFilter());
				final int result = chooser.showSaveDialog(this);
				if (result == JFileChooser.APPROVE_OPTION) {
					this.fileName = chooser.getSelectedFile().getAbsolutePath();
					if (!this.fileName.endsWith(".pdf")) {
						this.fileName = this.fileName + ".pdf";
					} else {
						this.fileName = "";
					}
				}
				this.setVisible(false);
			}
		}
	}

	public String getEndDate() {
		return DateUtil.formatSqlDate(this.endDate.getCalendar());
	}

	public String getFileName() {
		return this.fileName;
	}

	public String getGroupName() {
		return (String) this.reportSelection.getSelectedItem();
	}

	public ReportGroup getReportGroup() {
		return (ReportGroup) this.reportGroup.getSelectedItem();
	}

	public String getStartDate() {
		final Calendar cal = this.startDate.getCalendar();
		return DateUtil.formatSqlDate(cal);
	}

	public Volunteer getVolunteer() {
		return (Volunteer) this.reportSelection.getSelectedItem();
	}

	public boolean isSummary() {
		return this.summaryReport.isSelected();
	}

	private void initializeComponents() {
		this.setLayout(new BorderLayout());

		this.summaryReport = new JCheckBox();
		this.summaryReport.setName("Summary Report");

		this.startDate = new JCalendarCombo(JCalendarCombo.DISPLAY_DATE, false);
		this.startDate.setName("Start Date");
		this.startDate.setDate(DateUtil.getInstance("2011-01-01 12:00 AM").getTime());
		this.startDate.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

		this.endDate = new JCalendarCombo(JCalendarCombo.DISPLAY_DATE, false);
		this.endDate.setName("End Date");
		this.endDate.setDate(DateUtil.getInstance().getTime());
		this.endDate.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

		this.reportGroup = new JComboBox(ReportGroup.values());
		this.reportGroup.setName("Group");
		this.reportGroup.addActionListener(this);

		final String[] selections = { "" };
		this.reportSelection = new JComboBox(selections);
		this.reportSelection.setName("Selection");
		this.reportSelection.setEnabled(false);

		this.saveButton = new JButton("Print");
		this.saveButton.setName("");
		this.saveButton.addActionListener(this);

		this.add(new LabelPanel(FontUtil.getLabelFont(), Color.BLACK, this.summaryReport, this.startDate, this.endDate, this.reportGroup, this.reportSelection,
				this.saveButton));
		this.pack();
	}

	private boolean verify() {
		return DateUtil.isBefore(this.startDate.getCalendar(), this.endDate.getCalendar());
	}
}
